import router from './router';
import store from './store';
import {
  Message
} from 'element-ui';
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css'; // progress bar style
import {
  getToken
} from '@/utils/auth'; // get token from cookie
import {
  whiteList
} from './settings'; // get token from cookie

// eslint-disable-next-line no-unused-vars
import getPageTitle from '@/utils/get-page-title';

NProgress.configure({
  showSpinner: false
}); // NProgress Configuration

router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start();

  const hasToken = getToken();

  if (hasToken) {
    if (to.path === '/login') {
      next({
        path: '/'
      });
      NProgress.done();
    } else {
      next();
      return;
    }
  } else {
    // /login
    // console.log(whiteList);
    // console.log(to.path);
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next(`/login?redirect=${to.path}`);
      NProgress.done();
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});
